﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using SharedResources.Utilities;

namespace SharedResources.Model
{
    public class SelectableDataset : INotifyPropertyChanged
    {
        private string _dataLabel;

        public string DataLabel
        {
            get { return _dataLabel; }
            set 
            { 
                _dataLabel = value;
                OnPropertyChanged("DataLabel");
                RaiseNeedsSaving();
            }
        }

        public SortedGroupedDataset Dataset { get; set; }

        public SelectableDataset(SortedGroupedDataset dataset) //this is when the label is unknown
        {
            Dataset = dataset;
            DataLabel = GenerateDataLabel();
        }

        public SelectableDataset(string dataLabel, SortedGroupedDataset dataset)
        {
            DataLabel = dataLabel;
            Dataset = dataset;
        }

        private bool _willVisualise;
        public bool WillVisualise
        {
            get { return _willVisualise; } 
            set 
            { 
                _willVisualise = value;
                OnPropertyChanged("WillVisualise");
            }
        }

        private string GenerateDataLabel()
        {
            string generatedLabel;
            //Just uses the a default program selected name
            if (UnderlyingData.SelectableData.Count == 0)
            {
                generatedLabel = "new_dataset";
            }
            else
            {
                generatedLabel = UnderlyingData.SelectableData[0].Item1 + "_new";
            }
            return generatedLabel;
        }


        private void RaiseNeedsSaving()
        {
            SaveCurrent.NeedsSaving = true;
        }

        public IList<IList<Point>> GetVisualisationPoints()
        {
            //this method just gets all points for all times of the day
            IList<IList<Point>> pathDatas = new List<IList<Point>>();
            foreach (SortedList<DateTime, Point> sortedListPoints in Dataset.VisualisationPaths.Values)
            {
                pathDatas.Add(sortedListPoints.Values);
            }
            return pathDatas;
        }

        //GetVisualisaPoints(timeRange, region)

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
